package managers.messengerManager.services;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import managers.DBManager;
import managers.messengerManager.FeedbackManager;
import managers.messengerManager.MessengerManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import utils.StackTracePrinter;

public class ChangePersonalMessageService extends Service {

	private String message;

	public ChangePersonalMessageService(String message) {
		super("Update personal message");
		this.message = message;
	}


	@Override
	public void run() {

		Statement stmt;
		try {
			stmt = DBManager.getConnection().createStatement();
			String query = "UPDATE spotmessenger.users SET personalmessage= '"+message.replace("'", "\\'")+"' WHERE id= "+MessengerManager.getSharedInstance().getUserId();
			stmt.executeUpdate(query);
			DBManager.closeResources(stmt, null);
			MessengerManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.1"), FeedbackManager.DEFAULT);
			MessengerManager.getSharedInstance().getDataManager().retrievePersonalMessage();
			SocialNetworkManager.getSharedInstance().getFeedbackManager().updateElements();
		} catch (SQLException e) {
			StackTracePrinter.print(e);
			MessengerManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.2"), FeedbackManager.WARNING);
		} catch (Exception e) {
			StackTracePrinter.print(e);
			MessengerManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.3"), FeedbackManager.WARNING);
		}
	}



}
